<?php
    function buildSyntax($content, $regx)
    {
        preg_match($regx, $content, $match);        
        $dataId = !empty($match[2]) ? $match[2] : 0;
        $syntax = !empty($match[3]) ? $match[3] : "";        
        $userId = !empty($match[4]) ? $match[4] : 0;        
        
        $result = array();
        $result["userId"] = $userId;
        $result["syntax"] = $syntax;
        $result["dataId"] = $dataId;        
        
        return $result;
    }
    
    function buildSyntaxAppSmsKute($content, $regx)
    {
        preg_match($regx, $content, $match);        
        $dataId = !empty($match[2]) ? $match[2] : 0;        
        
        $user = getUserAppSmsKute($dataId);
        
        $result = array();
        $result["userId"] = !empty($user["userId"]) ? $user["userId"] : 0;
        $result["username"] = !empty($user["username"]) ? $user["username"] : "";
        $result["dataId"] = $dataId;        
        
        return $result;
    } 
    
    function getGameInfo($id)
    {
        $sql = "SELECT id, title, alias, isOnline, isSms, percent_wapmaster, percent_partner FROM c_game WHERE id=" . $id; 
        $result = @mysql_query($sql);
        $rows = @mysql_fetch_assoc($result);
        
        return $rows;
    }
    
    function getVideoInfo($id)
    {
        $sql = "SELECT id, title, alias FROM c_video WHERE id=" . $id . " AND status=1";        
        $result = @mysql_query($sql);
        $rows = @mysql_fetch_assoc($result);
        
        return $rows;
    }
    
    function getImageInfo($id)
    {
        $sql = "SELECT id, title, alias FROM c_album WHERE id=" . $id . " AND status=1";
        $result = @mysql_query($sql);
        $rows = @mysql_fetch_assoc($result);
        
        return $rows;
    }
    
    function getApp($type)
    {
        $sql = "SELECT * FROM c_app WHERE type=" . $type;
        $result = @mysql_query($sql);
        $rows = @mysql_fetch_assoc($result);
        
        return $rows;   
    }
    
    function getEvent($id)
    {
        $sql = "SELECT * FROM c_sms_kute_event WHERE id=" . $id;
        $result = @mysql_query($sql);
        $rows = @mysql_fetch_assoc($result);
        
        return $rows;   
    }
    
    function getUserInfo($id)
    {
        $sql = "SELECT id, username FROM c_user WHERE id=" . $id . " AND active=1 AND isBan=0";        
        $result = @mysql_query($sql);
        $rows = @mysql_fetch_assoc($result);
        
        return $rows;
    }
    
    function getUserAppSmsKute($id)
    {
        $sql = "SELECT * FROM c_app_sms_kute WHERE id=" . $id . " AND status=1";
        $result = @mysql_query($sql);
        $rows = @mysql_fetch_assoc($result);
        
        return $rows;
    }
    
    function getUrlUser($username) {
//        return $username . ".taoviec.com";
        return "cucdinh.mobi";
    }
           
    /*
    * processing
    */
    
    function processing($data)
    {        
        $regx_app_sms_kute = "/^TAI(\s+)AS(\d+)$/si";
        $regx = "/^TAI(\s+)(\d+)(G|V|H|S|K|D)(\d+)$/si";
        
        $list_service_code = array("6022");
        
        if(preg_match($regx, $data["content"]) || preg_match($regx_app_sms_kute, $data["content"]))
        {
            if(preg_match($regx, $data["content"])){
                $syntax = buildSyntax($data["content"], $regx);    
                
                if($syntax["syntax"]=="G"){
                    $data["type"] = 1;    
                } else if($syntax["syntax"]=="V"){
                    $data["type"] = 2;    
                } else if($syntax["syntax"]=="H"){
                    $data["type"] = 3;    
                } else if($syntax["syntax"]=="S"){
                    $data["type"] = 4;    
                } else if($syntax["syntax"]=="K"){
                    $data["type"] = 6;    
                } else if($syntax["syntax"]=="D"){
                    $data["type"] = 6;    
                }
                        
                $data["userId"] = $syntax["userId"];
                $data["id"] = $syntax["dataId"];            
                
                /*
                * Lấy thông tin về user 
                */
                
                $user = getUserInfo($data["userId"]);
                
                if(!empty($data["userId"]) && !empty($user) && !empty($data["id"]) && !empty($data["type"])){
                    if(in_array($data["serviceCode"], $list_service_code)){
                        
                        $username = $user["username"];
                        
                        $data["status"] = 1; 
                        
                        /*
                        * Nếu type là 2,3,4 thì trả về app video, ảnh, nhạc chờ tương ứng
                        * Nếu type là 1 thì kiểm tra
                        *   + Nếu là game offline thì trả về app game
                        *   + Nếu là game online hoặc sms thì trả về file game (nếu detect được) Hoặc trả về trang danh sách (nếu ko detect được)
                        */
                        
                        if($data["type"]==1){
                            
                            $game = getGameInfo($data["id"]);
                            if(!empty($game)){
                                if($game["isOnline"]==1 || $game["isSms"]==1){
                                    
                                    $data["percent_wapmaster"] = $game["percent_wapmaster"];
                                    $data["percent_partner"] = $game["percent_partner"];
                                    
                                    $url = getUrlUser($username) . "/dl/" . $username . "/" . $game["alias"] . "-" . $game["id"];    
                                } else {
                                    
                                    $data["percent_wapmaster"] = $game["percent_wapmaster"];
                                    $data["percent_partner"] = 0;
                                    
                                    $url = getUrlUser($username) .  "/download-game/" . $game["id"] . "/" . $game["alias"];
                                }
                                
                                $response["status"] = 1;                        
                                $response["msg"] = $url;
                            } else {
                                $response["status"] = 0;                        
                                $response["msg"] = "Khong tim thay noi dung duoc yeu cau";                                                                                                    
                            }
                                
                        } else {
                            if($syntax["syntax"]!="D"){
                                $app = getApp($data["type"]);
                                if(!empty($app)){
                                    $url = getUrlUser($username) .  "/" . $username . "/" . $app["alias"] . "-" . $app["id"];
                                    $response["status"] = 1;                        
                                    $response["msg"] = $url;    
                                } else {
                                    $response["status"] = 0;
                                    $response["msg"] = "Khong tim thay noi dung duoc yeu cau";
                                }    
                            } else {
                                $event = getEvent($data["id"]);    
                                if(!empty($event)){
                                    $url = getUrlUser($username) .  "/dlsk/" . $data["userId"] . "-" . $event["alias"] . "-sk" . $event["id"];                            
                                    $response["status"] = 1;                        
                                    $response["msg"] = $url;    
                                } else {
                                    $response["status"] = 0;
                                    $response["msg"] = "Khong tim thay noi dung duoc yeu cau";
                                }
                            }
                            $data["percent_wapmaster"] = 0;
                            $data["percent_partner"] = 0;
                        }
                                            
                    } else {
                        $response["status"] = 3;                        
                        $response["msg"] = "Tin nhan khong dung dau so quy dinh";                
                                            
                        $data["status"] = 0;                 
                    }
                       
                } else {
                    $response["status"] = 2;                
                    $response["msg"] = "Tin nhan sai cu phap";    
                    
                    $data["type"] = 0;
                    $data["userId"] = 0;                
                    $data["status"] = 0;
                }        
            } else if(preg_match($regx_app_sms_kute, $data["content"])){
                
                $data["type"] = 6;
                
                /*
                * cu phap tai cho tao ung dung sms kute
                */
                
                $syntax = buildSyntaxAppSmsKute($data["content"], $regx_app_sms_kute);   
                
                $data["userId"] = $syntax["userId"];
                $username = $syntax["username"];
                $data["id"] = $syntax["dataId"];
                
                if(!empty($data["userId"])){
                    if(in_array($data["serviceCode"], $list_service_code)){
                        $data["status"] = 1;
                        $url = getUrlUser($username) .  "/skdl/" . $username . "-a" . $data["id"];
                        
                        $response["status"] = 1;                        
                        $response["msg"] = $url;
                    } else {
                        $data["status"] = 0;   
                        
                        $response["status"] = 3;                        
                        $response["msg"] = "Tin nhan khong dung dau so quy dinh";                                                                            
                    }   
                } else {
                    
                    $data["status"] = 0;
                    $response["status"] = 0;
                    $response["msg"] = "Khong tim thay noi dung duoc yeu cau";    
                }
            }
        } else {
            $response["status"] = 2;                
            $response["msg"] = "Tin nhan sai cu phap";                
            
            $data["type"] = 0;
            $data["userId"] = 0;                
            $data["status"] = 0;      
        }
        
        /* log sms */
        $data["transactionId"] = $data["transactionId"];
        $data["telco"] = checkTelco($data["sender"]);    
        $data["price"] = priceServiceCode($data["serviceCode"]);    
        $data["partnerId"] = 1; //ID partner HDC            
        
        $data["output"] = $response["status"] . "|" . $data["transactionId"] . "|" . $response["msg"];            
        
        $data["percent_wapmaster"] = isset($data["percent_wapmaster"]) ? $data["percent_wapmaster"] : 0;
        $data["percent_partner"] = isset($data["percent_partner"]) ? $data["percent_partner"] : 0;        
        
        $result = InsertSmsLog($data);   
        return $response;
    }
?>
